<?php
declare(strict_types=1);
namespace App\ZxAdmin\Sys;
use App\Controller;

/**
 * @title	应用中心
 * @auth	true
 * @login	true
 * @menu	true
 * @component vlist/index.vue
 * @icon el-icon-menu
 * @rank    99
 */

// hook zxadmin_sys_plugin_use.php

Class Plugin extends Controller
{
	// hook zxadmin_sys_plugin_use.php

    public const Conf = [
        'Index' => 'dir',
        'pageSize' => 25,
        'pageSizes' => [10, 15, 20, 25, 30, 50, 100, 200, 500],
        'remoteSort' => 0,
        'remoteFilter' => 0,
        'remoteSummary' => 0,
        'hidePagination' => 1,
        'hideRefresh' => 0,
        'hideSetting' => 0,
        'hideDo' => 0,
        'add' => 0,
        'start' => 0,
        'stop' => 0,
        'delete' => 0,
        'import' => 0,
        'export' => 0
    ];

    public $plugin_cate=[
        [
            'Name' => '所有',
            'Index' => 'all'
        ],
        [
            'Name' => '功能',
            'Index' => 'hook'
        ],
        [
            'Name' => '主题',
            'Index' => 'view'
        ],
        [
            'Name' => '商城',
            'Index' => 'store'
        ],
    ];
    /**
     * @title  数据库连接
     * @rank    99
     */
    public function action_Conf_PUT()
    {
        // hook controller_zxadmin_sys_table_index_start.php
        $Columns = [
            [
                'type' => "dict",
                'label' => "状态",
                'prop' => "enable",
                'dict'=>'Status',
                'width' => "60px"
            ],
            [
                'label' => "名称",
                'prop' => "name",
                'width' => "300px"
            ],
            [
                'type' => "image",
                'label' => "图标",
                'prop' => "Patch",
                'width' => "60px"
            ],
            [
                'label' => "版本",
                'prop' => "version",
                'width' => "100px"
            ],
            [
                'label' => "描述",
                'prop' => "brief",
                'width' => "600px"
            ]];

        // hook controller_zxadmin_sys_table_index_end.php
        $this->response('0000', ['Tabs' => $this->plugin_cate,'Conf' => self::Conf, 'Columns' => $Columns]);
    }

    /**
	 * @title	插件管理
	 * @rank	99
	 */
	public function action_Index_GET()
	{
		plugin_init();
		$name = $this->request->param('name', '');
		$page = $this->request->param('page', 0);
		$cate = $this->request->param('tab', '');
        $cate == 'all' && $cate='';
		$limit = $this->request->param('limit', 0);
		$install = $this->request->param('install', 0);
		$disable = $this->request->param('disable', 0);
		$_plugins = $_ENV['plugins'];

		$install -= 1;
		$disable -= 1;
		$plugins = [];
		$i = 0;
		foreach ($_plugins as $plugin) {
			$i++;
			if ($name) {
				if (stripos($plugin['name'], $name) !== FALSE || stripos($plugin['brief'], $name) !== FALSE) {
					$plugins[$i] = $plugin;
				}
			} else {
				$plugins[$i] = $plugin;
			}
			if ($install >= 0) {
				if ($plugin['installed'] != $install) {
					unset($plugins[$i]);
				}
			}
			if ($disable >= 0) {
				if ($plugin['enable'] != $disable) {
					unset($plugins[$i]);
				}
			}
			if($cate){
              if(empty($plugin['cate']) || !in_array($cate,$plugin['cate'])){
                  unset($plugins[$i]);
              }
            }
            if(!empty($plugins[$i]['dir'])){
                $plugins[$i]['Patch']='../api/Plugin/'.$plugins[$i]['dir'].'/icon.png';
                unset($plugins[$i]['hooks']);
            }

		}
        $count = count($plugins);
        $page && $plugins = array_slice($plugins, ($page - 1) * $limit, $limit);
		$this->response('0000', ['data' => array_values($plugins),'total'=>$count]);
	}

	/**
	 * @title	安装
	 * @rank	99
	 */
	public function action_Install()
	{
		$dir = $this->request->param('dir');
		//$name = $_plugins[$dir]['name'];
		// 插件依赖检查 / check plugin dependency
        $array = plugin_check_dependency($dir, 'install');
        if($array!==true){
            $this->response('0001', ['data' => $array],'请先启用依赖插件');
        }
		// 安装插件 / install plugin  暂时还是手动吧
        plugin_install($dir);
		$this->response('0000', ['data' => ['install' => 1]]);
	}

    /**
     * @title   编辑字段
     * @rank    60
     */
    public function action_Field_POST()
    {
        // hook controller_zxadmin_content_cate_field_post_start.php
        $dir = $this->request->param('dir', "");
        $field = $this->request->param('field', '');
        $value = $this->request->param('value', '');
        $this->CheckEmpty([$field], ['待修改字段']);
        $field_fields = ['enable'];
        !in_array($field, $field_fields, true) && $this->response('0001', [], '字段不允许修改');
        if($value==1){
            $array = plugin_check_dependency($dir, 'enable');
            if($array!==true){
                $this->response('0001', ['data' => $array],'请先启用依赖插件');
            }
            plugin_enable($dir);

            is_dir(__PLUDIR__ . $dir . '/Static/') && Xcopy(__PLUDIR__ . $dir . '/Static/', __WEBDIR__ .'Plugin/'. $dir . '/');
            Xcopy(__PLUDIR__ . $dir . '/icon.png', __WEBDIR__ .'Plugin/'. $dir . '/icon.png');
        }else{
            plugin_disable($dir);
            if(is_dir(__WEBDIR__ .'Plugin/'. $dir . '/')){
                rmdir_recursive(__WEBDIR__ . 'Plugin/'.$dir .'/');
            }
        }

        // hook controller_zxadmin_content_cate_field_post_end.php
        $this->response('0000' , ['data' => '']);
    }

	// hook zxadmin_sys_plugin_end.php
}
?>